package org.jfree.chart.demo2;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;

import javax.swing.*;
import java.awt.*;
import java.util.Date;

public class GanttChartExample extends JFrame {

    public GanttChartExample(String title) {
        super(title);

        // 1. 设置全局字体 - 防止中文乱码
        setUIFont(new Font("SimSun", Font.PLAIN, 12));

        // 创建数据集
        TaskSeriesCollection dataset = createDataset();

        // 创建甘特图
        JFreeChart chart = ChartFactory.createGanttChart(
                "项目进度甘特图",       // 图表标题
                "任务",                // 横轴标签
                "时间",                // 纵轴标签
                dataset,              // 数据集
                true,                 // 显示图例
                true,                 // 显示提示
                false                // 不显示URL
        );

        // 2. 设置图表字体 - 防止图表中文乱码
        applyChineseFont(chart);

        // 设置图表面板
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(800, 600));
        setContentPane(chartPanel);
    }

    // 应用中文主题到图表
    private void applyChineseFont(JFreeChart chart) {
        // 创建支持中文的主题
        StandardChartTheme chartTheme = new StandardChartTheme("ChineseTheme");

        // 设置各种字体（使用支持中文的字体）
        Font extraLargeFont = new Font("SimSun", Font.BOLD, 18);
        Font largeFont = new Font("SimSun", Font.PLAIN, 15);
        Font regularFont = new Font("SimSun", Font.PLAIN, 13);
        Font smallFont = new Font("SimSun", Font.PLAIN, 11);

        chartTheme.setExtraLargeFont(extraLargeFont);  // 标题
        chartTheme.setLargeFont(largeFont);            // 图例
        chartTheme.setRegularFont(regularFont);        // 坐标轴标签
        chartTheme.setSmallFont(smallFont);            // 坐标轴刻度

        // 应用主题到图表
        chartTheme.apply(chart);

        // 设置任务名称字体（特殊处理）
        chart.getCategoryPlot().getDomainAxis().setTickLabelFont(regularFont);
    }

    // 设置全局UI字体
    private static void setUIFont(Font font) {
        java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof Font) {
                UIManager.put(key, font);
            }
        }
    }

    private TaskSeriesCollection createDataset() {
        TaskSeriesCollection collection = new TaskSeriesCollection();

        // 项目A
        TaskSeries projectA = new TaskSeries("项目A");
        Task task1 = new Task("需求分析",
            new SimpleTimePeriod(date(2023, 1, 1), date(2023, 1, 15)));
        Task task2 = new Task("设计阶段",
            new SimpleTimePeriod(date(2023, 1, 16), date(2023, 2, 5)));
        Task subtask = new Task("UI设计",
            new SimpleTimePeriod(date(2023, 1, 16), date(2023, 1, 25)));

        task2.addSubtask(subtask);
        projectA.add(task1);
        projectA.add(task2);

        // 项目B
        TaskSeries projectB = new TaskSeries("项目B");
        projectB.add(new Task("测试计划",
            new SimpleTimePeriod(date(2023, 1, 10), date(2023, 1, 30))));

        collection.add(projectA);
        collection.add(projectB);

        return collection;
    }

    // 辅助方法：创建日期对象
    private static Date date(int year, int month, int day) {
        return new Date(year - 1900, month - 1, day);
    }

    public static void main(String[] args) {
        // 3. 确保在事件调度线程中运行
        SwingUtilities.invokeLater(() -> {
            GanttChartExample example = new GanttChartExample("甘特图示例");
            example.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            example.pack();
            example.setLocationRelativeTo(null);
            example.setVisible(true);
        });
    }
}